﻿using Dal;
using Entity;
using Entity.DTO;
using IDal;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Principal;

namespace Bll
{
    /// <summary>
    /// 工程分类业务逻辑层
    /// </summary>
    public class ClassificationBll : IClassificationBll
    {
        IClassificationDal _classificationDal;
        IUserInfoDal _userInfoDal;

        public ClassificationBll(IClassificationDal classificationDal, IUserInfoDal userInfoDal)
        {
            _classificationDal = classificationDal;
            _userInfoDal = userInfoDal;
        }


        /// <summary>
        /// 创建工程分类
        /// </summary>
        /// <returns></returns>
        public bool CreateClassification(string title, string remark,  out string msg)
        {
            Classification classification = _classificationDal.GetDbSet()
                .Where(d => d.Title == title).FirstOrDefault();

            if (classification != null)
            {
                msg = "工程分类已经存在";
                return false;
            }

            Classification entity = new Classification();
            //创建guid 赋值给工程分类id，目的是保证id唯一
            entity.Id = Guid.NewGuid().ToString();
            entity.Remark = remark;
            entity.Title = title;
            entity.CreateTime = DateTime.Now;

            //调用数据访问层创建工程分类
            bool isSuccess = _classificationDal.CreateEntity(entity);

            if (isSuccess)
            {
                msg = "成功";
            }
            else
            {
                msg = "失败";
            }
            return isSuccess;
        }


        /// <summary>
        /// 软删除工程分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteClassification(string id)
        {
            Classification classification = _classificationDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (classification == null)
            {
                return false;
            }

            classification.IsDelete = true;

            return _classificationDal.UpdateEntity(classification);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteClassifications(List<string> ids)
        {
            foreach (var id in ids)
            {
                Classification classification = _classificationDal.GetDbSet().FirstOrDefault(u => u.Id == id);
                if (classification == null)
                {
                    return false;
                }

                classification.IsDelete = true;

                _classificationDal.UpdateEntity(classification);
            }

            return true;
        }


        /// <summary>
        /// 根据工程分类id获取工程分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetClassificationById(string id)
        {
            var data = _classificationDal.GetDbSet().FirstOrDefault(n => n.Id == id);

            if (data != null)
            {
                return new
                {
                    data.Remark,
                    data.Id,
                    data.Title,
                    CreateTime = data.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取工程分类集合
        /// </summary>
        public object GetClassificationList(string title, int page, int limit, out int count)
        {
            var queryable = from n in _classificationDal.GetDbSet().Where(n => !n.IsDelete)  //工程分类
                            select new
                            {
                                n.Id,
                                n.Title,
                                n.CreateTime,
                                n.Remark
                            };

            if (!string.IsNullOrEmpty(title))
            {
                queryable = queryable.Where(u => u.Title.Contains(title));
            }

            //查询总条数
            count = queryable.Count();

            var list = queryable.OrderByDescending(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(n=>new 
            {
                n.Id,
                n.Title,
                CreateTime =  n.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                n.Remark
            });

            return list;
        }



        /// <summary>
        /// 更新工程分类
        /// </summary>
        /// <returns></returns>
        public bool UpdateClassification(string id, string title, string remark, out string msg)
        {
            //根据账号找出工程分类的信息
            Classification entity = _classificationDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (entity == null)
            {
                msg = "未找到工程分类信息";
                return false;
            }

            //查询是否修改到其他的名字一样的情况
            Classification otherEntity = _classificationDal.GetDbSet().FirstOrDefault(u => u.Id != id && u.Title == title);
            if (otherEntity != null)
            {
                msg = "此工程分类名字已存在";
                return false;
            }

            entity.Title = title;
            entity.Remark = remark;

            //调用数据访问层
            bool isSucess = _classificationDal.UpdateEntity(entity);
            msg = "成功";
            return isSucess;
        }

        /// <summary>
        ///  获取下拉选备选数据
        /// </summary>
        /// <returns></returns>
        public object GetSelectOptionList()
        {
            return _classificationDal.GetDbSet().Where(u => !u.IsDelete).Select(d => new
            {
                d.Id,
                d.Title
            }).ToList();
        }



    }
}